# -*- coding:utf-8 -*-

from contextlib import closing
import MySQLdb
import MySQLdb.cursors
from DBUtils.PooledDB import PooledDB
from django.conf import settings

def mysql_pool(pool_size,
               db_name,
               db_user,
               db_password,
               db_host,
               db_port,
               charset="utf8"):

    return PooledDB(creator=MySQLdb, mincached=1, maxcached=pool_size,
            host=db_host,
            port=db_port,
            user=db_user,
            passwd=db_password,
            db=db_name,
            charset=charset,
            cursorclass=MySQLdb.cursors.DictCursor)

DB_POOL = None


def make_db_pool():
    global DB_POOL

    DB_POOL = mysql_pool(
        pool_size=settings.MYSQL_POOL_SIZE,
        db_name=settings.MYSQL_DB,
        db_user=settings.MYSQL_USER,
        db_password=settings.MYSQL_PASSWORD,
        db_host=settings.MYSQL_HOST,
        db_port=settings.MYSQL_PORT)


def get_conn():
    return DB_POOL.connection(shareable=False)


#make_db_pool()
